package com.example.nio.jvm;


import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import static java.util.stream.Collectors.*;

import java.util.Optional;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class StringExample {

    public static void main(String[] args) throws InterruptedException {
       /* List<Integer> list = Stream.of(1,2,3,4,5,7,8).collect(toList());
        Map<Boolean,List<Integer>> listMap = list.stream().collect(partitioningBy(i->i%2==0));
        System.out.println(listMap);
        Comparator<Integer> comparator = Comparator.comparingInt(Integer::valueOf);
        Optional<Integer> i = list.stream().collect(maxBy(comparator));
        System.out.println(i.get());
        System.out.println(list.stream().max(Comparator.comparingInt(Integer::valueOf)));

        */

       ThreadLocal<Integer> t = new ThreadLocal<>();
       t.set(1);
       Thread thread = new Thread(()->{
          t.set(2);
       });
       thread.start();
       System.out.println(t.get());

//        sumtest();
    }

    public static void sumtest() throws InterruptedException {
        System.out.println("开始插入");
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            list.add(i);
        }

        list.parallelStream().forEach(integer -> {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });





        System.out.println("插入完毕");
        long start = System.currentTimeMillis();
        long sum = list.parallelStream().mapToLong(x -> 1/0 ).sum();

        System.out.println("parallelStream："+(System.currentTimeMillis()-start)+"  sum="+sum);

        System.out.println("··············");
        long start2 = System.currentTimeMillis();
        long sum2 = list.stream().mapToLong(x -> x ).reduce(0,(x,y)->{
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return x+y;
        });
        System.out.println("stream："+(System.currentTimeMillis()-start2)+"  sum="+sum2);
    }
}
